"""
Case Type   : 全链路跟踪
Case Name   : 支持全链路跟踪,gsql直连方式查看net_trans_time记录
Create At   : 2024/10
Owner       : haomeng
Description :
    1、查看参数默认值
    2、设置enable_record_nettime为on，track_stmt_stat_level为'L0,L0'
    3、gsql直连执行部分DDL语句
    4、查看系统视图关键字段信息
    5、清理环境，恢复参数默认值
Expect      :
    1、参数默认值为off
    2、设置成功
    3、执行成功
    4、net_trans_time存在但统计结果为0
    5、恢复环境成功
History     :
"""

import os
import time
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro


class JDBCQuikAutoBalance(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.basename(__file__)} start----')
        self.comsh = CommonSH('PrimaryDbUser')
        self.com = Common('PrimaryDbUser')
        self.pri_root = Node('PrimaryRoot')
        self.pri_user = Node('PrimaryDbUser')
        self.constant = Constant()
        self.param1 = 'enable_record_nettime'
        self.param2 = 'track_stmt_stat_level'
        self.table_name = 't_jdbc_record_nettime_case0002'

    def test_record_nettime(self):
        text = '-----step1:查看enable_record_nettime参数默认值;  expect:默认值为off-----'
        self.log.info(text)

        value = self.comsh.execut_db_sql(f'show {self.param1}', dbname='postgres')
        self.param_default1 = value.splitlines()[-2].strip()
        self.log.info(self.param_default1)
        self.assertEqual('off', self.param_default1, 'nettime参数默认值校验失败')

        self.param_default2 = self.com.show_param(self.param2)
        self.log.info(self.param_default2)
        self.assertIn('OFF,L0', self.param_default2, 'track参数默认值校验失败')

        text = "-----step2:设置enable_record_nettime为on，track_stmt_stat_level为'L0,L0'  expect:设置成功-----"
        self.log.info(text)

        set_param1 = self.comsh.execute_gsguc('set',
                                              self.constant.GSGUC_SUCCESS_MSG,
                                              f'{self.param1} = on')
        self.log.info(set_param1)
        self.assertTrue(set_param1, '设置nettime参数失败')

        set_param2 = self.comsh.execute_gsguc('set',
                                              self.constant.GSGUC_SUCCESS_MSG,
                                              f"{self.param2} = 'L0,L0'")
        self.log.info(set_param2)
        self.assertTrue(set_param2, '设置track参数失败')

        restart_db = self.comsh.restart_db_cluster()
        self.log.info(restart_db)
        self.assertTrue(restart_db)

        text = "-----step3:gsql直连执行部分DDL语句   expect:执行成功-----"
        self.log.info(text)

        sql = f'create table {self.table_name}(id int);' \
              f'insert into {self.table_name} values(111),(222),(333);' \
              f'update {self.table_name} set id = 666;' \
              f'drop table {self.table_name} cascade;'
        self.log.info(sql)
        res = self.comsh.execut_db_sql(sql)
        self.log.info(res)
        self.assertIn(self.constant.TABLE_CREATE_SUCCESS, res, '建表失败')
        self.assertIn(self.constant.INSERT_SUCCESS_MSG, res, '插入数据失败')
        self.assertIn(self.constant.UPDATE_SUCCESS_MSG, res, '更新数据失败')
        self.assertIn(self.constant.TABLE_DROP_SUCCESS, res, '删表失败')

        text = '-----step4:查看系统视图关键字段信息  expect:net_trans_time存在但统计结果为0-----'
        self.log.info(text)

        sql1 = f"select sum(net_trans_time) from dbe_perf.statement where query like '%{self.table_name}%';"
        sql2 = f"select sum(net_trans_time) from dbe_perf.statement_history where query like '%{self.table_name}%';"

        res1 = self.comsh.execut_db_sql(sql1, dbname='postgres')
        self.log.info(res1)
        self.assertEqual(0, int(res1.splitlines()[-2].strip()), '查询结果不为0')

        res2 = self.comsh.execut_db_sql(sql2, dbname='postgres')
        self.log.info(res2)
        self.assertEqual(0, int(res2.splitlines()[-2].strip()), '查询结果不为0')

    def tearDown(self):
        text = '-----step5:恢复参数默认值,恢复环境  expect:恢复成功-----'
        self.log.info(text)

        cmd = f"drop table if exists {self.table_name};" \
              f"select reset_unique_sql('global', 'all', 2);" \
              f"delete from dbe_perf.statement_history;"
        clear_view = self.comsh.execut_db_sql(cmd, dbname='postgres')
        self.log.info(clear_view)

        reset_param1 = self.comsh.execute_gsguc('set',
                                                self.constant.GSGUC_SUCCESS_MSG,
                                                f'{self.param1} = {self.param_default1}')
        self.log.info(reset_param1)

        reset_param2 = self.comsh.execute_gsguc('set',
                                                self.constant.GSGUC_SUCCESS_MSG,
                                                f"{self.param2} = '{self.param_default2}'")
        self.log.info(reset_param2)

        restart = self.comsh.restart_db_cluster()
        self.log.info(restart)

        reset_param_value1 = self.comsh.execut_db_sql(f'show {self.param1}', dbname='postgres')
        self.log.info(reset_param_value1)
        reset_param_value2 = self.com.show_param(self.param2)
        self.log.info(reset_param_value2)

        self.assertEqual(self.param_default1, reset_param_value1.splitlines()[-2].strip(), '恢复nettime默认值失败')
        self.assertEqual(self.param_default2, reset_param_value2, '恢复track默认值失败')
        self.assertTrue(restart)
        self.log.info(f'----{os.path.basename(__file__)} end----')
